/** @file    NetworkProtocolUdp.cpp
 *  @author  Alessandro Polo
 *  @version $Id: NetworkProtocolUdp.cpp 2829 2010-08-02 20:45:47Z alex $
 *  @brief
 * File containing methods for the wosh::network::NetworkProtocolUdp class.
 * The header for this class can be found in NetworkProtocolUdp.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <framework/network/NetworkProtocolUdp.h>

 #include <core/ObjectFactory.h>
 #include <framework/network/NetworkUtilities.h>


using namespace std;
namespace wosh {
 namespace network {

 WOSH_REGISTER(wosh::network::NetworkProtocolUdp, "wosh::NetworkProtocol", 1.01, _static_NetworkProtocolUdp)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

NetworkProtocolUdp::NetworkProtocolUdp()
	: wosh::NetworkProtocol() {
	Object::setName( _NetworkProtocolUdp_NAME );
	Log.setContext( _NetworkProtocolUdp_NAME );

	Properties.set( _NetworkProtocol_KEY_ProtocolPrefix, _NetworkProtocolUdp_PREFIX );
	Properties.set( _NetworkProtocol_KEY_Priority, _NetworkProtocolUdp_Priority );
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool NetworkProtocolUdp::supportProtocol( const std::string& protocol_string ) const {
	if ( protocol_string.size() < strlen(_NetworkProtocolUdp_PREFIX)+1 ) return false;
	if ( protocol_string.substr(0, strlen(_NetworkProtocolUdp_PREFIX)) != _NetworkProtocolUdp_PREFIX ) return false;
	return true;
 }


WRESULT NetworkProtocolUdp::listenChannel( const std::string& protocol_string, unsigned long timeout ) {
	if ( !supportProtocol(protocol_string) ) return WRET_ERR_PARAM;
	std::string addressport = protocol_string.substr(6);
	wosh::network::NetworkChannelUdp* chs = new wosh::network::NetworkChannelUdp("NetworkChannelUdp#" + addressport );
	WRESULT ret = chs->listen(addressport, timeout);
	if ( this->listener != NULL )
		this->listener->network_protocol_supported(protocol_string, NULL, this);
	if ( this->listener != NULL )
		this->listener->network_protocol_new_channel(chs, 0, this);
	return ret;
 }

WRESULT NetworkProtocolUdp::connectChannel( const std::string& protocol_string, unsigned long timeout ) {
	if ( !supportProtocol(protocol_string) ) return WRET_ERR_PARAM;
	std::string addressport = protocol_string.substr(6);
	wosh::network::NetworkChannelUdp* ch = new wosh::network::NetworkChannelUdp("NetworkChannelUdp#" + addressport);
	WRESULT ret = ch->connect(addressport, timeout);
	if ( this->listener != NULL )
		this->listener->network_protocol_new_channel(ch, 0, this);
	return ret;
 }

WRESULT NetworkProtocolUdp::connectChannel( const std::vector<std::string>& protocolsAvailable, unsigned long timeout ) {
	if ( protocolsAvailable.size() == 0 ) return WRET_ERR_PARAM;
	if ( protocolsAvailable.size() == 1 ) {
		return connectChannel( protocolsAvailable.at(0), timeout );
	 }
	return WRET_ERR_PARAM;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace network
}; // namespace wosh
